package workflowRoute

import (
	"project-info/src/handle/workflow"
	"project-info/src/lib"
	"project-info/src/router/req"
	"project-info/src/router/variable"

	"github.com/gin-gonic/gin"
)

func init() {
	variable.InitRouteFunc = append(variable.InitRouteFunc, InitWorkflowRoute)
}

func InitWorkflowRoute(e *gin.Engine) {
	workflowGroup := req.NewGroup("/workflow").NeedToken().SetUpGroup(e)

	reqs := []*req.Conf{
		// 工单配置相关
		req.NewPost("/save", workflow.SaveWorkflow),
		req.NewPost("/page", workflow.PageWorkflow),
		req.NewDelete("/delete", workflow.DeleteWorkflow),
		req.NewPost("/list", workflow.GetWorkflowList),
		req.NewPost("/start", workflow.StartWorkflow),
		// 节点相关
		req.NewPost("/node/approve", workflow.ApproveWorkflowNode).WithMiddleware(needPermissionJudgeMiddleware()),
		req.NewPost("/node/reject", workflow.RejectWorkflowNode).WithMiddleware(needPermissionJudgeMiddleware()),
		req.NewPost("/node/comment", workflow.CommentWorkflowNode).WithMiddleware(needPermissionJudgeMiddleware()),
		req.NewPost("/node/transfer", workflow.TransferWorkflowNode).WithMiddleware(needPermissionJudgeMiddleware()),
		req.NewPost("/node/admin/approve", workflow.ApproveWorkflowNode),
		req.NewPost("/node/admin/reject", workflow.RejectWorkflowNode),
		req.NewPost("/node/admin/comment", workflow.CommentWorkflowNode),
		// 工单实例相关
		req.NewPost("/instance/cancel", workflow.CancelWorkflowInstance).WithMiddleware(needPermissionJudgeMiddleware()),
		req.NewPost("/instance/admin/cancel", workflow.CancelWorkflowInstance),
		req.NewPost("/instance/startByMyself", workflow.FindStartByMyself),
		req.NewPost("/instance/findPenddingApprove", workflow.FindPenddingApprove),
		req.NewPost("/instance/findApprovedByMe", workflow.FindApprovedByMyself),
		req.NewPost("/instance/findById", workflow.FindWorkflowInstanceById),
		req.NewPost("/instance/findDetailById", workflow.FindWorkflowInstanceDetailById),
		req.NewPost("/instance/findAll", workflow.FindAllInstance),
	}
	req.BatchSetUpRouter(workflowGroup, reqs)
}

// 操作节点需要鉴权的中间件
func needPermissionJudgeMiddleware() lib.MiddlewareFunc {
	return func(c *lib.GinContext) error {
		c.Set(workflow.NeedPermissionJudge, true)
		return nil
	}
}
